package com.game.modular.game.battle.process.skillEffectHandle.impl;

import com.game.modular.game.battle.panel.attribute.domain.EffectStatus;
import com.game.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import com.game.modular.game.battle.panel.skillEffect.PvpSkillEffectServiceImpl;
import com.game.modular.game.battle.panel.skillEffect.domain.SkillEffectStatus;
import com.game.modular.game.battle.process.skillEffectHandle.base.BaseSkillEffectStrategy;
import com.game.modular.game.battle.panel.skillEffect.PvpSkillEffect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Map;

@Slf4j
public class ControlStrategy {
    @Component
    public static class BaseControlStrategy extends BaseSkillEffectStrategy {
        @Override
        public PvpSkillEffect applyEffect(PvpSkillEffect pvpSkillEffect) {
            return pvpSkillEffect;
        }

        public void obtainEffectStatus(PvpSkillEffect pvpSkillEffect) {
            Long battleId = pvpSkillEffect.getBattleId();
            Long coreSkillEffectTypeId = pvpSkillEffect.getCoreSkillEffectTypeId();
            Long targetPlayerHeroId = pvpSkillEffect.getTargetPlayerHeroId();
            PlayerHeroAttribute targetAttribute = heroAttributeService.get(battleId, targetPlayerHeroId);
            Map<Long, EffectStatus> status = targetAttribute.getStatus();
            EffectStatus effectStatus = status.get(coreSkillEffectTypeId);
            if (effectStatus == null) {
                effectStatus = EffectStatus.builder()
                        .status(coreSkillEffectTypeId)
                        .coreSkillType(pvpSkillEffect.getCoreSkillTyp())
                        .coreSkillEffectId(pvpSkillEffect.getCoreSkillEffectId())
                        .baseKeep(pvpSkillEffect.getBaseTrigger().getKeep())
                        .start(pvpSkillEffect.getRound())
                        .baseUseNum(pvpSkillEffect.getBaseTrigger().getTriggerNum())
                        .useNum(0)
                        .refreshRound(new HashSet<>())
                        .magicInfluence(pvpSkillEffect.getMagicInfluence()/100.0)
                        .skillEffect(pvpSkillEffect)
                        .build();
                status.put(23L, effectStatus);
                targetAttribute.setStatus(status);
                heroAttributeService.saveCache(targetAttribute);
            }else {
                SkillEffectStatus status1 = pvpSkillEffect.getStatus();
                status1.setIsTrigger(0);
            }
        }
    }

    // 23	混乱	无法行动（控制状态）				21
    @Component
    public static class Control_23 extends BaseControlStrategy {
        @Resource
        private PvpSkillEffectServiceImpl skillEffectService;
        @Override
        public PvpSkillEffect applyEffect(PvpSkillEffect pvpSkillEffect) {
            effectHandle2.tryTriggerJudgment(pvpSkillEffect);
            effectHandle2.logTriggerFailReasons(pvpSkillEffect);
            if (pvpSkillEffect.getStatus().getIsTrigger() == 1) {
                super.obtainEffectStatus(pvpSkillEffect);
                skillEffectService.remove(pvpSkillEffect);
            }
            if (pvpSkillEffect.getStatus().getIsSave() == 1){
                skillEffectService.save(pvpSkillEffect);
            }
            return pvpSkillEffect;
        }

    }

    // 24	缴械	无法普通攻击（控制状态）				21
    @Component
    public static class Control_24 extends BaseControlStrategy {
        @Resource
        private PvpSkillEffectServiceImpl skillEffectService;
        @Override
        public PvpSkillEffect applyEffect(PvpSkillEffect pvpSkillEffect) {
            effectHandle2.tryTriggerJudgment(pvpSkillEffect);
            effectHandle2.logTriggerFailReasons(pvpSkillEffect);
            if (pvpSkillEffect.getStatus().getIsTrigger() == 1) {
                super.obtainEffectStatus(pvpSkillEffect);
                skillEffectService.remove(pvpSkillEffect);
            }
            if (pvpSkillEffect.getStatus().getIsSave() == 1){
                skillEffectService.save(pvpSkillEffect);
            }
            return pvpSkillEffect;
        }
    }

    // 25	技穷	无法主动战法（控制状态）				21
    @Component
    public static class Control_25 extends BaseControlStrategy {
        @Resource
        private PvpSkillEffectServiceImpl skillEffectService;
        @Override
        public PvpSkillEffect applyEffect(PvpSkillEffect pvpSkillEffect) {
            effectHandle2.tryTriggerJudgment(pvpSkillEffect);
            effectHandle2.logTriggerFailReasons(pvpSkillEffect);
            if (pvpSkillEffect.getStatus().getIsTrigger() == 1) {
                super.obtainEffectStatus(pvpSkillEffect);
                skillEffectService.remove(pvpSkillEffect);
            }
            if (pvpSkillEffect.getStatus().getIsSave() == 1){
                skillEffectService.save(pvpSkillEffect);
            }
            return pvpSkillEffect;
        }
    }

    // 26	暴走	无法正确攻击目标(改成敌我全体)				21
    @Component
    public static class Control_26 extends BaseControlStrategy {
        @Resource
        private PvpSkillEffectServiceImpl skillEffectService;
        @Override
        public PvpSkillEffect applyEffect(PvpSkillEffect pvpSkillEffect) {
            effectHandle2.tryTriggerJudgment(pvpSkillEffect);
            effectHandle2.logTriggerFailReasons(pvpSkillEffect);
            if (pvpSkillEffect.getStatus().getIsTrigger() == 1) {
                super.obtainEffectStatus(pvpSkillEffect);
                skillEffectService.remove(pvpSkillEffect);
            }
            if (pvpSkillEffect.getStatus().getIsSave() == 1){
                skillEffectService.save(pvpSkillEffect);
            }
            return pvpSkillEffect;
        }
    }

    // 54	嘲讽	无法正确普通攻击目标(改为嘲讽自己的对象)				21
    @Component
    public static class Control_54 extends BaseControlStrategy {
        @Resource
        private PvpSkillEffectServiceImpl skillEffectService;
        @Override
        public PvpSkillEffect applyEffect(PvpSkillEffect pvpSkillEffect) {
            effectHandle2.tryTriggerJudgment(pvpSkillEffect);
            effectHandle2.logTriggerFailReasons(pvpSkillEffect);
            if (pvpSkillEffect.getStatus().getIsTrigger() == 1) {
                super.obtainEffectStatus(pvpSkillEffect);
                skillEffectService.remove(pvpSkillEffect);
            }
            if (pvpSkillEffect.getStatus().getIsSave() == 1){
                skillEffectService.save(pvpSkillEffect);
            }
            return pvpSkillEffect;
        }
    }
}
